/**
 * Mastra客户端Agent功能测试
 */

import { MastraClient } from '@mastra/client-js';

console.log('🚀 开始Mastra客户端Agent功能验证...\n');

const client = new MastraClient({
  baseUrl: 'http://localhost:4111',
  retries: 3,
  backoffMs: 300,
  maxBackoffMs: 5000,
});

console.log('📡 Mastra客户端已创建，连接到:', 'http://localhost:4111');

async function testGameMasterAgent() {
  console.log('\n📋 1. 测试游戏主控Agent...');
  
  try {
    console.log('   🔄 调用游戏主控Agent...');
    
    const result = await client.agent.run({
      agentId: 'gameMasterAgent',
      messages: [
        {
          role: 'user',
          content: `请开始一个新的赛博侦探游戏。游戏配置：难度normal，主题办公室，3个嫌疑人。请使用 game-master 工具执行 start_game 操作。`
        }
      ]
    });

    console.log('   ✅ 游戏主控Agent响应成功！');
    console.log('   📝 响应文本长度:', result.text?.length || 0);
    console.log('   🔧 工具调用数量:', result.toolCalls?.length || 0);
    
    if (result.toolCalls && result.toolCalls.length > 0) {
      console.log('   🎯 工具调用成功');
    }
    
    return { success: true, result };
  } catch (error) {
    console.log('   ❌ 游戏主控Agent测试失败');
    console.log('   📝 错误:', error.message);
    return { success: false, error };
  }
}

async function testSuspectDialogueAgent() {
  console.log('\n📋 2. 测试嫌疑人对话Agent...');
  
  try {
    console.log('   🔄 调用嫌疑人对话Agent...');
    
    const result = await client.agent.run({
      agentId: 'suspectDialogueAgent',
      messages: [
        {
          role: 'user',
          content: `请模拟一个谨慎内向的秘书角色，回答侦探的问题："你当时在哪里？"请使用 dialogue-management 工具生成回应。`
        }
      ]
    });

    console.log('   ✅ 嫌疑人对话Agent响应成功！');
    console.log('   📝 响应文本长度:', result.text?.length || 0);
    console.log('   🔧 工具调用数量:', result.toolCalls?.length || 0);
    
    return { success: true, result };
  } catch (error) {
    console.log('   ❌ 嫌疑人对话Agent测试失败');
    console.log('   📝 错误:', error.message);
    return { success: false, error };
  }
}

async function runTests() {
  console.log('🎯 开始Agent功能验证...\n');
  
  const results = {
    gameMaster: await testGameMasterAgent(),
    suspectDialogue: await testSuspectDialogueAgent(),
  };

  const successCount = Object.values(results).filter(r => r?.success).length;
  const totalTests = Object.keys(results).length;

  console.log('\n' + '='.repeat(50));
  console.log('📊 测试结果报告');
  console.log('='.repeat(50));
  console.log(`🎯 成功率: ${successCount}/${totalTests} (${Math.round(successCount/totalTests*100)}%)`);
  console.log(`🤖 游戏主控Agent: ${results.gameMaster?.success ? '✅' : '❌'}`);
  console.log(`💬 嫌疑人对话Agent: ${results.suspectDialogue?.success ? '✅' : '❌'}`);

  if (successCount === totalTests) {
    console.log('\n🎉 所有Agent功能验证通过！');
  } else {
    console.log('\n⚠️  部分Agent需要调试');
  }

  return results;
}

runTests().catch(error => {
  console.error('❌ 测试失败:', error);
  process.exit(1);
});
